package com.application.tourapp;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.application.tourapp.dal.CityDAL;
import com.application.tourapp.dal.TypeDAL;
import com.application.tourapp.global.VaraibleGlobal;
import com.application.tourapp.model.City;
import com.application.tourapp.model.Type;

/**
 * Un ecran pour le paramtrage de l'application
 * 
 * @author Islam
 * 
 */

public class RechercheActivity extends GenericActivity {
	CityDAL cityDAL;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.recherche_activity);
		setHeader(getResources().getString(R.string.Recherche), true, true);

		ArrayList<Type> typeDetails = GetSearchResults(getApplicationContext());

		ArrayList<String> typeName = new ArrayList<String>();
		for (int i = 0; i < typeDetails.size(); i++) {
			typeName.add(typeDetails.get(i).getName());
		}

		final Spinner spinnerTypeName = (Spinner) findViewById(R.id.spinnerItemName);
		ArrayAdapter<String> dataAdapterTypeName = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, typeName);
		dataAdapterTypeName
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTypeName.setAdapter(dataAdapterTypeName);
		

		cityDAL = new CityDAL(getApplicationContext());
		final Spinner spinnerVilleName = (Spinner) findViewById(R.id.spinnerVilleName);
		ArrayAdapter<City> dataAdapterVilleName = new ArrayAdapter<City>(this,
				android.R.layout.simple_spinner_item, cityDAL.getAllCities());
		dataAdapterVilleName
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerVilleName.setAdapter(dataAdapterVilleName);

		Button btnRechercher = (Button) findViewById(R.id.btnRechercher);
		btnRechercher.setOnClickListener(new OnClickListener() {

			
			@Override
			public void onClick(View arg0) {
				
				final City selectedCity = (City) spinnerVilleName.getSelectedItem();
				VaraibleGlobal.setCity(selectedCity);
				
				final String  selectedType = (String) spinnerTypeName.getSelectedItem();
				VaraibleGlobal.setTypeName(selectedType);
				
				 Intent intent = new Intent(getApplicationContext(), AfficherRecherche.class);
				 startActivity(intent);	
			}
		});
		
	}
	
	public ArrayList<Type> GetSearchResults(Context context) {
		return new TypeDAL(context).getAlltypes();
	}

}
